package cn.wolfcode.wms.domain;

import cn.wolfcode.wms.util.JSONUtil;
import cn.wolfcode.wms.util.StringUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.io.FilenameUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Getter@Setter@ToString
public class Product extends BaseDomain{
    private String name;
    private String sn;
    private BigDecimal costPrice;
    private BigDecimal salePrice;
    private String imagePath; //货品图片路径
    private String intro;   //备注
    private Long brandId;

    private String brandName; //冗余数据,品牌名称,因为品牌名称不会改变

    public String getImagePath(){
        return StringUtil.empty2null(imagePath);
    }

    public String getSmallImagePath(){
        //拿到文件的扩展名
        // /uplaod/xxxxxxx.jpg
        // /uplaod/xxxxxxx_small.jpg
        String ext = FilenameUtils.getExtension(imagePath);
        String path = imagePath.substring(0, imagePath.lastIndexOf("."));
        return path + "_small." + ext;
    }

    //自定义属性转化成JSON
    public String getJsonString() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        map.put("costPrice", costPrice);
        map.put("salePrice", salePrice);
        map.put("brandName", brandName);
        return JSONUtil.toJSONString(map);
    }

}